#---------------------------------------感知机的实现与局限性
#与门的简单实现
# def AND(x1,x2):
#     w1,w2,theta = 0.5,0.5,0.7
#     tmp = x1*w1 + x2*w2
#     if tmp <= theta:
#         return 0
#     elif tmp > theta:
#         return 1

# print(AND(0,0))
# print(AND(1,0))
# print(AND(0,1))
# print(AND(1,1))

#导入权重和偏置
import numpy as np
def AND(x1,x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.7
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1
    
# #非门
def NAND(x1,x2):
    x = np.array([x1,x2])
    w = np.array([-0.5,-0.5])
    b = 0.7
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1
#或门
def OR(x1,x2):
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.2
    tmp = np.sum(w*x) + b
    if tmp <= 0:
        return 0
    else:
        return 1


#-------------------------------------多层感知机
#异或门的实现-两层感知机的组合
def XOR(x1,x2):
    s1 = NAND(x1,x2)
    s2 = OR(x1,x2)
    y = AND(s1,s2)
    return y

print(XOR(1,1))
print(XOR(0,1))
print(XOR(1,0))
print(XOR(0,0))
